package com.rainbow.dynamic.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtils {

	private static ObjectMapper mapper = new ObjectMapper();

	static {
		// 序列化的时候序列对象的所有属性
		mapper.setSerializationInclusion(Include.ALWAYS);

		// 反序列化的时候如果多了其他属性,不抛出异常
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

		// 如果是空对象的时候,不抛异常
		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

		// 取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
		mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	}

	/**
	 * 对象转为Json字符串
	 * 
	 * @param data
	 * @return
	 */
	public static String objToJson(Object data) {
		String jsonStr = null;
		try {
			jsonStr = mapper.writeValueAsString(data);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return jsonStr;
	}

	/**
	 * 对象转为byte数组
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] objToBytes(Object data) {
		byte[] byteArr = null;
		try {
			byteArr = mapper.writeValueAsBytes(data);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return byteArr;
	}

	/**
	 * json字符串转为对象
	 * 
	 * @param str
	 * @param valueType
	 * @return
	 */
	public static <T> T jsonToObj(String jsonStr, Class<T> valueType) {
		if (StringUtils.isEmpty(jsonStr)) {
			return null;
		}
		T data = null;
		try {
			data = mapper.readValue(jsonStr, valueType);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * byte数组转为对象
	 * 
	 * @param byteArr
	 * @param valueType
	 * @return
	 */
	public static <T> T bytesToObj(byte[] byteArr, Class<T> valueType) {
		T data = null;
		try {
			data = mapper.readValue(byteArr, valueType);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 将json数据转换成pojo对象list
	 * <p>
	 * Title: jsonToList
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param jsonData
	 * @param beanType
	 * @return
	 */
	public static <T> List<T> jsonToList(String jsonStr, Class<T> beanType) {
		if (StringUtils.isEmpty(jsonStr)) {
			return null;
		}
		JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, beanType);
		try {
			List<T> list = mapper.readValue(jsonStr, javaType);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
